<template>
  <div class="article-list" ref="box" @scroll="onScroll">
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      :success-text="isreFreshText"
      success-duration="1500"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <!-- <van-cell v-for="(item, i) in list" :key="i" :title="item.title" /> -->
        <article-item
          v-for="(item, i) in list"
          :key="i"
          :article="item"
        ></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article.js';
import ArticleItem from '@/components/ArticleItem.vue';

let scrollTop = 0;

export default {
  name: 'ArticleList',
  components: { ArticleItem },
  props: {
    channel: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: null,
      error: false,
      count: 0,
      isLoading: false,
      isreFreshText: '更新成功',
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  activated() {
    this.$refs.box.scroll(0, scrollTop);
  },
  methods: {
    onScroll(e) {
      scrollTop = e.target.scrollTop;
    },
    onRefresh() {
      getArticles({
        channel_id: this.channel.id,
        timestamp: Date.now(),
        with_top: 1,
      })
        .then((res) => {
          const { results } = res.data.data;
          this.list.unshift(...results);
          this.isreFreshText = '更新成功,更新了' + results.length + '条数据';
          this.isLoading = false;
        })
        .catch(() => {
          this.isreFreshText = '刷新失败';
          this.isLoading = false;
        });
    },
    onLoad() {
      getArticles({
        channel_id: this.channel.id,
        timestamp: this.timestamp || Date.now(),
        with_top: 1,
      })
        .then((res) => {
          const { results } = res.data.data;
          this.list.push(...results);
          // 加载状态结束
          this.loading = false;
          // 数据全部加载完成
          if (results.length) {
            this.timestamp = res.data.data.pre_timestamp;
          } else {
            this.finished = true;
          }
        })
        .catch(() => {
          // 展示错误提示
          this.error = true;
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="less" scoped>
.article-list {
  height: 80vh;
  overflow-y: auto;
}
/deep/ .article-item.van-cell {
  display: flex;
  justify-self: center;

  .van-cell__title {
    flex: auto;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .van-cell__value {
    flex: unset;
    width: 232px;
    height: 146px;
    padding-left: 25px;
  }
  .title {
    font-size: 32px;
  }
  .van-cell__label {
    font-size: 23px;
  }
  .van-cell__label {
    span {
      margin-right: 10px;
    }
  }
}
</style>
